package com.sushuo.api.web;


import java.io.Serializable;

public class Result implements Serializable {
    
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 9012806816596216834L;
    
    public static final int ERROR = -1;// 服务器内部错误
    
    public static final int SUCCESS = 0;// 成功
    
    public static final String SUCCESS_MESSAGE = "success";
    
    public static final String ERROR_MESSAGE = "fail";
    
    private int code = SUCCESS;
    
    private String message = SUCCESS_MESSAGE;
    
    private Object data = "{}"; // 没值用空 json
    
    public Result() {
    }
    
    public Result(StatusCode statusCode) {
        super();
        this.code = statusCode.getValue();
        this.message = statusCode.getMsg();
    }
    
    public Result(int code, String message) {
        super();
        this.code = code;
        this.message = message;
    }
    
    public Result(int code, String message, Object data) {
        super();
        this.code = code;
        this.message = message;
        if (null!=data) {
            this.data = data;
        }
    }
    
    public static Result success(Object data) {
        return new Result(SUCCESS, SUCCESS_MESSAGE, data);
        
    }
    
    public static Result successMsg(String message){
        return new Result(SUCCESS,message);
    }
    
    public static Result success() {
        return new Result(SUCCESS, SUCCESS_MESSAGE);
        
    }
    
    public static Result error(Object data) {
        return new Result(ERROR, ERROR_MESSAGE, data);
    }
    
    public static Result error(String message){
        return new Result(ERROR, message);
    }
    
    public int getCode() {
        return code;
    }
    
    public void setCode(int code) {
        this.code = code;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public Object getData() {
        return data;
    }
    
    public void setData(Object data) {
        this.data = data;
    }
    
    /**
     * 传入自定义状态码枚举对象
     * @param enumObj
     * @return
     * @author chentaikuang
     *2017年12月21日
     */
    public static Result custom(Enum<?> enumObj) {
        StatusCode statusCode = null;
        if (enumObj instanceof StatusCode) {
            statusCode = (StatusCode)enumObj;
        }else {
            statusCode = StatusCode.ENUM_ERR;
        }
        return new Result(statusCode.getValue(), statusCode.getMsg());
    }
}
